package org.digitaldomination.thefinalcountdown;

import java.util.Calendar;
import java.util.concurrent.TimeUnit;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TimePicker;
import android.widget.TabHost.TabSpec;

public class Addcountdown extends Activity implements OnClickListener {

	private Button countdownpickdate, countdownpicktime, counttopickdate,
			counttopicktime, countprogresspicktodate, countprogresspicktotime,
			countprogressbtninsert, countprogresspickfromdate,
			countprogresspickfromtime;
	private EditText countdowntitle, counttotitle, countprogresstitle;
	private Button countdownbtninsert, counttobtninsert;
	private CheckBox countdowncheckcheckbox, counttocheckcheckbox,
			countprogresscheckcheckbox;
	private String addcountdowntitle;
	private long date, date_to, count_type, count_type_from, count_type_to;
	private int mYear, mMonth, mDay, mHour, mMinute, sYear, sMonth, sDay, sHour, sMinute;

	static final int TIME_DIALOG_ID = 1;
	static final int DATE_DIALOG_ID = 0;
	static final int TIME2_DIALOG_ID = 3;
	static final int DATE2_DIALOG_ID = 4;

	final DBAdapter db = new DBAdapter(this);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addcountdown);
		initialize();
	}

	public void initialize() {

		createtab();

		countdowntitle = (EditText) findViewById(R.id.etcountdowntoevent);
		countdownpickdate = (Button) findViewById(R.id.btntodate);
		countdownpicktime = (Button) findViewById(R.id.btntotime);
		countdowncheckcheckbox = (CheckBox) findViewById(R.id.cbtotime);
		countdownbtninsert = (Button) findViewById(R.id.btntodateinsert);

		counttotitle = (EditText) findViewById(R.id.etfromdateevent);
		counttopickdate = (Button) findViewById(R.id.btnfromdate);
		counttopicktime = (Button) findViewById(R.id.btnfromtime);
		counttocheckcheckbox = (CheckBox) findViewById(R.id.cbfromdatetime);
		counttobtninsert = (Button) findViewById(R.id.btnfromdateinsert);

		countprogresstitle = (EditText) findViewById(R.id.etprogressevent);
		countprogresspicktodate = (Button) findViewById(R.id.btnprogresstodate);
		countprogresspicktotime = (Button) findViewById(R.id.btnprogresstotime);
		countprogresspickfromdate = (Button) findViewById(R.id.btnprogressfromdate);
		countprogresspickfromtime = (Button) findViewById(R.id.btnprogressfromtime);
		countprogresscheckcheckbox = (CheckBox) findViewById(R.id.cbprogresstime);
		countprogressbtninsert = (Button) findViewById(R.id.btnprogressinsert);

		countdownpickdate.setOnClickListener(this);
		countdownpicktime.setOnClickListener(this);
		countdowncheckcheckbox.setOnClickListener(this);
		countdownbtninsert.setOnClickListener(this);

		counttopickdate.setOnClickListener(this);
		counttopicktime.setOnClickListener(this);
		counttocheckcheckbox.setOnClickListener(this);
		counttobtninsert.setOnClickListener(this);

		countprogresspicktodate.setOnClickListener(this);
		countprogresspicktotime.setOnClickListener(this);
		countprogresspickfromdate.setOnClickListener(this);
		countprogresspickfromtime.setOnClickListener(this);
		countprogresscheckcheckbox.setOnClickListener(this);
		countprogressbtninsert.setOnClickListener(this);

		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);
		mHour = c.get(Calendar.HOUR_OF_DAY);
		mMinute = c.get(Calendar.MINUTE);
		final Calendar cd = Calendar.getInstance();
		sYear = cd.get(Calendar.YEAR);
		sMonth = cd.get(Calendar.MONTH);
		sDay = cd.get(Calendar.DAY_OF_MONTH);
		sHour = cd.get(Calendar.HOUR_OF_DAY);
		sMinute = cd.get(Calendar.MINUTE);

		updateDisplay();
		checkcheckbox("to");
		checkcheckbox("from");
		checkcheckbox("progress");
	}

	private void createtab() {
		TabHost th = (TabHost) findViewById(R.id.tabhost);
		th.setup();
		TabSpec specs = th.newTabSpec("tag1");
		specs.setContent(R.id.tab1);
		specs.setIndicator("Countdown to");
		th.addTab(specs);

		specs = th.newTabSpec("tag2");
		specs.setContent(R.id.tab2);
		specs.setIndicator("Count from");
		th.addTab(specs);

		specs = th.newTabSpec("tag3");
		specs.setContent(R.id.tab3);
		specs.setIndicator("Count event");
		th.addTab(specs);
	}

	private void checkcheckbox(String check) {

		if (check.equals("to")) {
			if (countdowncheckcheckbox.isChecked()) {
				countdownpicktime.setVisibility(View.VISIBLE);
				updateDisplay();
			} else {
				countdownpicktime.setVisibility(View.GONE);
			}

		}

		else if (check.equals("from")) {
			if (counttocheckcheckbox.isChecked()) {
				counttopicktime.setVisibility(View.VISIBLE);
				updateDisplay();
			} else {
				counttopicktime.setVisibility(View.GONE);
			}
		}

		else if (check.equals("progress")) {
			if (countprogresscheckcheckbox.isChecked()) {
				countprogresspicktotime.setVisibility(View.VISIBLE);
				countprogresspickfromtime.setVisibility(View.VISIBLE);
				updateDisplay();
			} else {
				countprogresspicktotime.setVisibility(View.GONE);
				countprogresspickfromtime.setVisibility(View.GONE);
			}
		}

	}

	private void updateDisplay() {

		countdownpickdate.setText(new StringBuilder().append(pad((mMonth + 1)))
				.append("/").append(pad(mDay)).append("/").append(mYear)
				.append(" "));

		if (countdowncheckcheckbox.isChecked()) {
			countdownpicktime.setText(new StringBuilder().append(pad(mHour))
					.append(":").append(pad(mMinute)));
		}

		counttopickdate.setText(new StringBuilder().append(pad((mMonth + 1)))
				.append("/").append(pad(mDay)).append("/").append(mYear)
				.append(" "));

		if (counttocheckcheckbox.isChecked()) {
			counttopicktime.setText(new StringBuilder().append(pad(mHour))
					.append(":").append(pad(mMinute)));
		}

		countprogresspickfromdate.setText(new StringBuilder()
				.append(pad((mMonth + 1))).append("/").append(pad(mDay))
				.append("/").append(mYear).append(" "));

		if (countprogresscheckcheckbox.isChecked()) {
			countprogresspickfromtime.setText(new StringBuilder()
					.append(pad(mHour)).append(":").append(pad(mMinute)));
		}

		countprogresspicktodate.setText(new StringBuilder()
				.append(pad((sMonth + 1))).append("/").append(pad(sDay))
				.append("/").append(sYear).append(" "));

		if (countprogresscheckcheckbox.isChecked()) {
			countprogresspicktotime.setText(new StringBuilder()
					.append(pad(sHour)).append(":").append(pad(sMinute)));
		}
	}

	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;
			updateDisplay();
		}
	};

	private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			mHour = hourOfDay;
			mMinute = minute;
			updateDisplay();
		}
	};
	
	private DatePickerDialog.OnDateSetListener sDateSetListener = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			sYear = year;
			sMonth = monthOfYear;
			sDay = dayOfMonth;
			updateDisplay();
		}
	};

	private TimePickerDialog.OnTimeSetListener sTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			sHour = hourOfDay;
			sMinute = minute;
			updateDisplay();
		}
	};

	private static String pad(int c) {
		if (c >= 10)
			return String.valueOf(c);
		else
			return "0" + String.valueOf(c);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
					mDay);
		case TIME_DIALOG_ID:
			return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute,
					true);
		case DATE2_DIALOG_ID:
			return new DatePickerDialog(this, sDateSetListener, sYear, sMonth,
					sDay);
		case TIME2_DIALOG_ID:
			return new TimePickerDialog(this, sTimeSetListener, sHour, sMinute,
					true);
		}
		return null;
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btntodateinsert:
			writecountdown("to");
			break;
		case R.id.cbtotime:
			checkcheckbox("to");
			break;
		case R.id.btntotime:
			showDialog(TIME_DIALOG_ID);
			break;
		case R.id.btntodate:
			showDialog(DATE_DIALOG_ID);
			break;
		case R.id.btnfromdateinsert:
			writecountdown("from");
			break;
		case R.id.cbfromdatetime:
			checkcheckbox("from");
			break;
		case R.id.btnfromtime:
			showDialog(TIME_DIALOG_ID);
			break;
		case R.id.btnfromdate:
			showDialog(DATE_DIALOG_ID);
			break;
		case R.id.btnprogressinsert:
			writecountdown("progress");
			break;
		case R.id.cbprogresstime:
			checkcheckbox("progress");
			break;
		case R.id.btnprogresstotime:
			showDialog(TIME2_DIALOG_ID);
			break;
		case R.id.btnprogresstodate:
			showDialog(DATE2_DIALOG_ID);
			break;
		case R.id.btnprogressfromtime:
			showDialog(TIME_DIALOG_ID);
			break;
		case R.id.btnprogressfromdate:
			showDialog(DATE_DIALOG_ID);
			break;
		}
	}

	public void writecountdown(String input) {
		
		Calendar cal = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		cal.set(mYear, mMonth, mDay, mHour, mMinute, 00);
		cal2.set(sYear, sMonth, sDay, sHour, sMinute, 00);
		date = cal.getTimeInMillis();
		date_to = cal2.getTimeInMillis();
		date = TimeUnit.MILLISECONDS.toSeconds(date);
		date_to = TimeUnit.MILLISECONDS.toSeconds(date_to);
		
		if (input.equals("to")) {
			addcountdowntitle = countdowntitle.getText().toString();
			count_type = 1;
			count_type_from = date;
			count_type_to = 0; 
		} else if (input.equals("from")) {
			addcountdowntitle = counttotitle.getText().toString();
			count_type = 2;
			count_type_from = 0;
			count_type_to = date;
		} else if (input.equals("progress")) {
			addcountdowntitle = countprogresstitle.getText().toString();
			count_type = 3;
			count_type_from = date;
			count_type_to = date_to;
		} else {
			addcountdowntitle = "";
		}

		if (countdowncheckcheckbox.isChecked() == false || counttocheckcheckbox.isChecked() == false || countprogresscheckcheckbox.isChecked() == false) {
			mHour = 00;
			mMinute = 00;
		}

		db.open();
		db.queryaddcountdown(count_type_from, count_type_to, count_type, addcountdowntitle);
		db.close();
		finish();
	}
}